<template>
	<view>
		<view class="order-header">
			<view class="u-p-30 bg-white filter-panel-search">
				<view class="u-flex">
					<view class="u-flex-1">
						<u-search v-model="key" placeholder="搜索" :show-action="false" shape="square" clearabled @change="getList(true)"></u-search>
					</view>
					<view class="u-p-l-30">
						<button size="mini" @click="add" class="u-h-64" style="display: block;">
							<view class="u-flex u-align-items u-justify-content u-h-60">
								<u-icon name="plus" size="28" class="u-tips-color"></u-icon>
								<text class="u-p-l-10 u-tips-color">新建</text>
							</view>
						</button>
					</view>
				</view>
			</view>
		</view>
		<view class="grid-box" v-for="(item, index) in list" :key="index">
			<navigator class="order" @click="details(item)">
				<view class="u-p-b-20 u-flex">
					<view class="u-flex-1">
						<view class="u-flex">
							<text class="u-font-32 u-p-r-20 u-flex-1" v-if="parentId == 0">{{item.name}}</text>
							<text class="u-font-32 u-p-r-20 u-flex-1" v-if="parentId > 0">{{item.department}}</text>
							<!-- <u-tag :text="item.type" type="success" size="mini"/> -->
							<u-icon name="arrow-right"></u-icon>
						</view>
						<view class="u-p-t-8">
							<view class="u-flex u-font-24">
								<text class="u-font-24 u-tips-color">所属加盟商：{{item.franchise.name}}</text>
							</view>
						</view>
					</view>
				</view>
		
				<view class="bottom u-border-top u-p-t-20">
					<view>
						<text class="u-font-24 u-tips-color">所属地区：{{item.citynames}}</text>
					</view>
					<view>
						<text class="u-font-24 u-tips-color" v-if="item.user">绑定账号：{{ item.user.name || '--' }}{{ item.parentid == 0?'(管理员)':'' }}</text>
					</view>
					<view class="more">
						<view class="u-flex">
							<view class="u-flex-1">
								<text class="u-font-24 u-tips-color">{{item.regdate}}注册</text>
							</view>
							<u-icon name="clock" class="u-tips-color"></u-icon>
							<text class="u-font-24 u-tips-color">{{$u.timeFrom(new Date(item.regdate), false)}}</text>
						</view>
					</view>
				</view>
			</navigator>
		</view>
		<view class="u-p-30">
			<u-loadmore :status="loadStatus" />
		</view>
	</view>
</template>

<script>
	import {
		mapState,
	} from 'vuex';
	
	export default {
		props: {
			sendFranchiseId: {
				type: [Number, String],
				default: null
			},
			parentId:{
				type: [Number, String],
				default: 0
			}
		},
		data() {
			return {
				loadStatus: 'loadmore',
				PageIndex: 1,
				PageSize: 20,
				key: '',
				cityId: 0, //地区
				// 时间类型
				timeType: 0, //筛选时间类别：0全部，1今日，2昨日，3本月，4上月，5本季度，6上季度，7本年，9自定义时间段
				startDate: '', //开始时间
				endDate: '', //结束时间
				// franchiseId: 0, //所属加盟商
				list: []
			}
		},
		onPullDownRefresh() {
			this.orderList = []
			this.PageIndex = 1
			this.getList().then(() => {
				uni.stopPullDownRefresh(); //停止当前页面下拉刷新
			})
		},
		onReachBottom(e) {
			this.reachBottom()
		},
		onLoad() {
			this.getList(true)
		},
		mounted() {
			console.log('parentId',this.parentId)
			this.getList(true)
		},
		computed: {
			...mapState({
				franchiseId: state => state.franchiseId
			})
		},
		methods: {
			reachBottom() {
				// 此tab为空数据
				this.PageIndex += 1
				if (this.loadStatus == 'loadmore') {
					this.getList()
				}
			},
			// 页面数据
			getList(reload = false) {
				if(reload){
					this.PageIndex = 1
					this.list = []
				}
				this.loadStatus = "loading"
				
				return this.$u.post('/Enterprise/List', {
					"PageIndex": this.PageIndex,
					"PageSize": this.PageSize,
					"Key": this.key,
					// "timeType": this.timeType, //筛选时间类别：0全部，1今日，2昨日，3本月，4上月，5本季度，6上季度，7本年，9自定义时间段
					// "startDate": this.startDate, //开始时间
					// "endDate": this.endDate, //结束时间
					// "order": [
					// 	{
					// 		"column": "regdate",
					// 		"isDesc": true
					// 	}
					// ],
					// "type": null,
					"parentId": this.parentId,
					// "cityId": this.cityId,
					"franchiseId": this.sendFranchiseId || this.franchiseId || 0,
					
					// "PageIndex":1,"PageSize":10,"Key":"","timeType":0,"startDate":"","endDate":"","order":[{"column":"regdate","isDesc":true}],"type":null,"parentId":0,"cityId":0,"franchiseId":1
				}).then((res) => {
					if (res.rows.length > 0) {
						res.rows.forEach((item) => {
							if(item.icon != ''){
								item.icon = this.fileURL + item.icon	
							}
							this.list.push(item)
						})
						if (res.rows.length < this.PageSize) {
							this.loadStatus = "nomore"
						} else {
							this.loadStatus = "loadmore"
						}
					} else {
						this.loadStatus = "nomore"
					}
					
					return res.rows
				})
			},
			add(){
				if(this.parentId > 0){
					uni.navigateTo({
						url: '/pages/admin/franchise/enterprise/create/create?parentId='+this.parentId
					})
				}else{
					let id = this.sendFranchiseId || this.franchiseId
					uni.navigateTo({
						url: '/pages/admin/franchise/enterprise/create/create?franchiseId='+id
					})
				}
			},
			details(item){
				uni.navigateTo({
					url: '/pages/admin/franchise/enterprise/views/views?id='+item.id
				})
			}
		}
	}
</script>

<style>

</style>
